package com.ng.desktop.hr.view.cases;

import javax.swing.JOptionPane;

import com.ng.desktop.hr.model.Cases;
import com.ng.desktop.hr.persistence.CasePersistence;
import com.ng.framework.util.NGUtil;
import com.ng.framework.view.desktop.NGTablePanel;

public class CaseTablePanel extends NGTablePanel<Cases, CasePersistence> {

	public CaseTablePanel(CasePersistence persistence) {
		super(persistence);
	}

	private static final long serialVersionUID = -3041915458364809210L;

	private static final String[] COLUMN_NAMES = {"Id", "Titulo", "Cliente", "Estado", "Prioridad", "Contacto", "Creación",
			"Resolución Estimada", "Resolución Real"};

	//45 menos que el width
	private static final int[] COLUMN_WIDTHS = {70, 220, 159, 70, 70, 70, 100, 120, 100};

	@Override
	protected String[] getColumnNames() {
		return COLUMN_NAMES;
	}

	@Override
	protected int[] getColumnWidths() {
		return COLUMN_WIDTHS;
	}

	@Override
	protected Object[] toRow(Cases customerCase) {
		return new Object[]{
				customerCase.getId(),
				customerCase.getTitle(),
				customerCase.getCustomer().getFullName(),
				customerCase.getCaseState().getDescription(),
				customerCase.getCasePriority().getDescription(),
				customerCase.getCaseContactType().getDescription(),
				customerCase.getCreationDate() == null ? "" : NGUtil.ddMMyyyy_dateFormat.format(customerCase.getCreationDate()),
				customerCase.getEstimatedResolutionDate() == null ? "" : NGUtil.ddMMyyyy_dateFormat.format(customerCase
						.getEstimatedResolutionDate()),
				customerCase.getRealResolutionDate() == null ? "" : NGUtil.ddMMyyyy_dateFormat.format(customerCase
						.getRealResolutionDate())};
	}

	@Override
	protected boolean create(CasePersistence persistence) {
		CaseEditDialog dialog = new CaseEditDialog(JOptionPane.getFrameForComponent(this), persistence);
		dialog.setLocationRelativeTo(this);
		dialog.setVisible(true);
		return true;
	}

	@Override
	protected boolean edit(CasePersistence persistence, Cases instance) {
		CaseEditDialog dialog = new CaseEditDialog(JOptionPane.getFrameForComponent(this), persistence, instance);
		dialog.setLocationRelativeTo(this);
		dialog.setVisible(true);
		return true;
	}

	@Override
	protected Cases getSelectedInstance(CasePersistence persistence, Object[] rowData) {
		return persistence.findById((Long) rowData[0]);
	}
}
